package com.xbongbong.workflow.service;

import com.xbongbong.paas.pojo.workflow.operate.dto.WorkflowApproveFailedDTO;
import com.xbongbong.paas.pojo.workflow.operate.dto.WorkflowUpdateBackDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.workflow.service.strategy.data.WorkflowDataProcessStrategy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/08/23 11:04
 */
@Service("workflowDataProcessService")
public class WorkflowDataProcessServiceImpl implements WorkflowDataProcessService {

    @Resource
    private Map<Integer, WorkflowDataProcessStrategy> workflowDataProcessStrategyMap;

    @Override
    public void approvalFailed(WorkflowApproveFailedDTO workflowApproveFailedDTO) throws XbbException {
        // 新建， 拒绝时删除主数据
        Integer businessType = XbbRefTypeEnum.PAAS.getCode();
        if (Objects.equals(SaasMarkEnum.SAAS.getCode(), workflowApproveFailedDTO.getSaasMark())) {
            businessType = workflowApproveFailedDTO.getBusinessType();
        }
        WorkflowDataProcessStrategy workflowDataProcessStrategy = workflowDataProcessStrategyMap.get(businessType);
        workflowDataProcessStrategy.approvalFailed(workflowApproveFailedDTO.getTaskId(), workflowApproveFailedDTO.getWorkflowTransferPOJO(), workflowApproveFailedDTO.getTaskType());
    }

    @Override
    public void updateBack(WorkflowUpdateBackDTO workflowUpdateBackDTO) throws XbbException {
        // 编辑（还原编辑进审批前数据）
        Integer businessType = XbbRefTypeEnum.PAAS.getCode();
        if (Objects.equals(SaasMarkEnum.SAAS.getCode(), workflowUpdateBackDTO.getSaasMark())) {
            businessType = workflowUpdateBackDTO.getBusinessType();
        }
        WorkflowDataProcessStrategy workflowDataProcessStrategy = workflowDataProcessStrategyMap.get(businessType);
        workflowDataProcessStrategy.updateBack(workflowUpdateBackDTO.getTaskId(), workflowUpdateBackDTO.getWorkflowTransferPOJO());
    }
}
